using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using B.S.RbacData.ErrorCode;
using SupplierCustomerManagement.Write.API.Applications.Commands.productpurchaseprices;

namespace SupplierCustomerManagement.Write.API.Controllers
{
    /// <summary>
    /// 商品采购价格管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ProductPurchasePricesController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IMapper _mapper;
        private readonly ILogger<ProductPurchasePricesController> _logger;

        public ProductPurchasePricesController(
            IMediator mediator,
            IMapper mapper,
            ILogger<ProductPurchasePricesController> logger)
        {
            _mediator = mediator;
            _mapper = mapper;
            _logger = logger;
        }

        /// <summary>
        /// 添加商品采购价格
        /// </summary>
        /// <param name="command">商品采购价格信息</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProductPurchasePrice([FromBody] AddProductPurchasePriceCommand command)
        {
            try
            {
                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功添加商品采购价格，ID: {PriceId}, 供应商ID: {SupplierId}, 商品: {ProductName}", 
                        result.data, command.SupplierId, command.ProductName);
                }
                else
                {
                    _logger.LogWarning("添加商品采购价格失败: {Message}, 供应商ID: {SupplierId}, 商品: {ProductName}", 
                        result.message, command.SupplierId, command.ProductName);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "添加商品采购价格时发生异常，供应商ID: {SupplierId}, 商品: {ProductName}", 
                    command.SupplierId, command.ProductName);
                return ApiResult<int>.Error($"添加商品采购价格时发生异常: {ex.Message}");
            }
        }

        /// <summary>
        /// 修改商品采购价格
        /// </summary>
        /// <param name="command">商品采购价格修改信息</param>
        /// <returns>修改结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> UpdateProductPurchasePrice([FromBody] UpdateProductPurchasePriceCommand command)
        {
            try
            {
                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功修改商品采购价格，ID: {PriceId}, 供应商ID: {SupplierId}, 商品: {ProductName}", 
                        command.Id, command.SupplierId, command.ProductName);
                }
                else
                {
                    _logger.LogWarning("修改商品采购价格失败: {Message}, ID: {PriceId}, 供应商ID: {SupplierId}, 商品: {ProductName}", 
                        result.message, command.Id, command.SupplierId, command.ProductName);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "修改商品采购价格时发生异常，ID: {PriceId}, 供应商ID: {SupplierId}, 商品: {ProductName}", 
                    command.Id, command.SupplierId, command.ProductName);
                return ApiResult<bool>.Error($"修改商品采购价格时发生异常: {ex.Message}");
            }
        }

        /// <summary>
        /// 删除商品采购价格
        /// </summary>
        /// <param name="command">删除商品采购价格信息</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<ApiResult<bool>> DeleteProductPurchasePrice([FromBody] DeleteProductPurchasePriceCommand command)
        {
            try
            {
                // 发送命令
                var result = await _mediator.Send(command);

                if (result.IsSuccess)
                {
                    _logger.LogInformation("成功删除商品采购价格，ID: {PriceId}", command.Id);
                }
                else
                {
                    _logger.LogWarning("删除商品采购价格失败: {Message}, ID: {PriceId}", 
                        result.message, command.Id);
                }

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除商品采购价格时发生异常，ID: {PriceId}", command.Id);
                return ApiResult<bool>.Error($"删除商品采购价格时发生异常: {ex.Message}");
            }
        }
    }
} 